﻿using Enum;
using MediatR;
using Practice.Common.Domains;
using Practice.Common.Dto;
using Practice.Common.Infrastructure.Interfaces;

namespace Practice.API.Application.Commands.Permissionw
{
    public class PermissionDelHandler : IRequestHandler<PermissionDelCommand, Result>
    {
        public readonly IBaseRepository<Permission> PermissionRepository;

        public PermissionDelHandler(IBaseRepository<Permission> permissionRepository)
        {
            PermissionRepository = permissionRepository;
        }

        public async Task<Result> Handle(PermissionDelCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.Success };
            try
            {
                var res = PermissionRepository.GetAll().Where(x => x.PermissionId == request.PermissionId).FirstOrDefault();
                if (res != null)
                {
                    res.IsDelete = true;
                    PermissionRepository.Update(res);
                    result.Code = (int)CommonEnumCode.Success;
                }
                return result;

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
